SHELL = /bin/bash

repo="jinmz"
app="cdce-service"
PKGMAP=Mcommon/common.proto=gitee.com/jinmingzhi/cdce-service/api/protos/common


run:
	./build/service

proto:
	@for t in common/v1alpha cost/v1alpha desc/v1alpha exam/v1alpha participate/v1alpha; do \
    	protoc -Iapi/protos -I${GOPATH}/src -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
    		-I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate \
    		-I ${GOPATH}/src/github.com/golang/protobuf/protoc-gen-go \
    		-I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway \
    		--go_out=plugins=grpc,paths=source_relative:api/protos \
    		--validate_out=lang=go,paths=source_relative:api/protos \
    		api/protos/$$t/*.proto; \
    	if ls api/protos/$$t/public*.proto 1> /dev/null 2>&1;  then protoc -Iapi/protos -I${GOPATH}/src -I${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis \
    		-I ${GOPATH}/src/github.com/envoyproxy/protoc-gen-validate \
    		-I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway \
        	--grpc-gateway_out=logtostderr=true,paths=source_relative:api/protos \
        	--swagger_out=logtostderr=true:api/protos \
        	 api/protos/$$t/public*.proto; fi \
	done

test:
	go test ./...

vet:
	go vet ./...

build: vet
	@go build -ldflags="-s -w" -o build/service ./cmd/service/
	@go build -ldflags="-s -w" -o build/migration ./cmd/migration/

swg:
	@rm -f pkg/swagger/proto/*
	@cp api/protos/*/*/*.swagger.json pkg/swagger/proto/
	@go-bindata -pkg static -o pkg/static/datafile.go pkg/swagger/...

docker-build:
	@echo "build docker image"
	@BRANCH_NAME=$(shell git rev-parse --abbrev-ref HEAD); \
	if [[ $$BRANCH_NAME == "develop" ]]; then \
		ENV="stage"; \
	elif [[ $$BRANCH_NAME == "master" ]]; then \
		ENV="prod"; \
	else \
		ENV="feat"; \
	fi; \
	docker build --build-arg GITEE_USER=$$GITEE_USER --build-arg GITEE_TOKEN=$$GITEE_TOKEN -t ${repo}/${app}:$$ENV .


docker-push:
	@echo "tag & docker push"
	@BRANCH_NAME=$(shell git rev-parse --abbrev-ref HEAD); SHA1_SHORT=$(shell git rev-parse --short HEAD); \
	if [[ $$BRANCH_NAME == "develop" ]]; then \
		ENV="stage"; \
	elif [[ $$BRANCH_NAME == "master" ]]; then \
		ENV="prod"; \
	else \
		ENV="feat"; \
	fi;  \
	docker tag  ${repo}/${app}:$$ENV   ${repo}/${app}:$$SHA1_SHORT; \
	docker push  ${repo}/${app}:$$SHA1_SHORT; \
	docker push  ${repo}/${app}:$$ENV;
